#include "StateManager.h"

std::vector<CGameState*> CStateManager::GetListState()
{
	return m_list_state;
}

//Make sure only one StateManager exist in game
CStateManager* CStateManager::m_instance = 0;

CStateManager* CStateManager::GetInstance()
{
	if (m_instance == 0)
	{
		m_instance = new CStateManager();
	}

	return m_instance;
}

void CStateManager::ChangeState(CGameState* state)
{
	//Clear current state
	if (!m_list_state.empty())
	{
		m_list_state.back()->Release();
		m_list_state.pop_back();
	}

	//Add new state
	m_list_state.push_back(state);
	m_list_state.back()->Init();
}

void CStateManager::PushState(CGameState* state)
{
	//Pause the current state
	if (!m_list_state.empty())
	{
		m_list_state.back()->Pause();
	}

	//Add new state
	m_list_state.push_back(state);
	m_list_state.back()->Init();
}

void CStateManager::PopState()
{
	//Clear current state
	if (!m_list_state.empty())
	{
		m_list_state.back()->Release();
		m_list_state.pop_back();
	}

	//Resume the previous state
	if (!m_list_state.empty())
	{
		m_list_state.back()->Resume();
	}
}

void CStateManager::ClearAllState()
{
	while (!m_list_state.empty())
	{
		m_list_state.back()->Release();
		m_list_state.pop_back();
	}
}
